﻿using System.Collections.Generic;

namespace Red.Controls.Tile
{
    public class Callback
    {
        private Dictionary<string, List<Handler>> _clickHandlers = new Dictionary<string, List<Handler>>();

        public delegate void Handler( string function );

        public void Register( string group, Handler handler )
        {
            List<Handler> handlers;
            if ( !_clickHandlers.TryGetValue( group, out handlers ) )
            {
                handlers = new List<Handler>();
                _clickHandlers.Add( group, handlers );
            }

            handlers.Add( handler );
        }

        public bool Invoke( string group, string function )
        {
            if ( !string.IsNullOrWhiteSpace( group ) && !string.IsNullOrWhiteSpace( function ) )
            {
                List<Handler> handlers;
                if ( _clickHandlers.TryGetValue( group, out handlers ) )
                {
                    foreach ( Handler handler in handlers )
                    {
                        handler( function );
                    }

                    return true;
                }
            }

            return false;
        }
    }
}
